package com.zonta.finance.serivce.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zonta.finance.common.dto.PageInfo;
import com.zonta.finance.common.service.TokenService;
import com.zonta.finance.common.utils.DateUtil;
import com.zonta.finance.config.ObjectConvertor;
import com.zonta.finance.domain.dto.AdminDTO;
import com.zonta.finance.domain.form.*;
import com.zonta.finance.domain.po.AccountBook;
import com.zonta.finance.domain.vo.GetAccountBookVo;
import com.zonta.finance.domain.vo.ListAccountBookVo;
import com.zonta.finance.mapper.AccountBookMapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zonta.finance.serivce.IAccountBookService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 账套 服务实现类
 * </p>
 *
 * @author zonta
 * @since 2024-07-28
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class AccountBookServiceImpl extends ServiceImpl<AccountBookMapper, AccountBook> implements IAccountBookService {
    private final ObjectConvertor objectConvertor;
    private final TokenService<AdminDTO> tokenService;
    /**
     * 根据id查询账套信息
     * @param id
     * @return
     */
    @Override
    public GetAccountBookVo getByIdVo(long id) {
        AccountBook accountBook = getById(id);
        return objectConvertor.toGetAccountBookVo(accountBook);
    }

    /**
     * 分页条件查询数据
     * @param form
     * @return
     */
    @Override
    public PageInfo<ListAccountBookVo> getListByFrom(ListAccountBookForm form) {
        LambdaQueryWrapper<AccountBook> wrapper = new LambdaQueryWrapper<>();
        if (form.getDisable()!=null){
            wrapper.eq(AccountBook::getDisable,form.getDisable());
        }

        if (StrUtil.isNotEmpty(form.getCompanyName())){
            wrapper.like(AccountBook::getCompanyName,form.getCompanyName());
        }
        wrapper.eq(AccountBook::getTenantId,tokenService.getThreadLocalTenantId());
        Page<AccountBook> page = page(new Page<>(form.getPageNum(), form.getPageSize()),wrapper);
        PageInfo<ListAccountBookVo> bookVoPage = objectConvertor.toListAccountBookVoPage(page);

        return bookVoPage;
    }

    /**
     * 禁用或者启用账套
     * @param form
     * @return
     */
    @Override
    public Boolean disable(AccountBookDisableForm form) {
        LambdaUpdateWrapper<AccountBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(AccountBook::getDisable,form.getDisable())
                .eq(AccountBook::getId,form.getId());

        return update(wrapper);
    }

    /**
     * 创建租户
     * @param form
     * @return
     */
    @Override
    public Boolean saveByFrom(AddAccountBookForm form) {
        AccountBook accountBook = objectConvertor.handleSaveAccountBook(form);
        accountBook.setMemberId(tokenService.getThreadLocalUserId());
        accountBook.setTenantId(tokenService.getThreadLocalTenantId());
        accountBook.initDefault();
        return save(accountBook);
    }
    /**
     * 删除账套
     *
     * @param form 账套
     * @return 结果
     */
    @Override
    public Boolean del(DelForm form) {
        LambdaUpdateWrapper<AccountBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(AccountBook::getDelFlag,true)
                .set(AccountBook::getUpdateMemberId,tokenService.getThreadLocalUserId())
                .set(AccountBook::getUpdateTime, DateUtil.getSystemTime())
                .eq(AccountBook::getId,form.getId())
                .eq(AccountBook::getTenantId, tokenService.getThreadLocalTenantId());
        return update(wrapper);
    }

    /**
     * 编辑账套
     * @param form
     * @return
     */
    @Override
    public Boolean updateByFrom(UpdateAccountBookForm form) {
        AccountBook accountBook = BeanUtil.copyProperties(form, AccountBook.class);
        LambdaQueryWrapper<AccountBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AccountBook::getId,form.getId());
        wrapper.eq(AccountBook::getTenantId,tokenService.getThreadLocalTenantId());
        return update(accountBook,wrapper);
    }

}
